<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>Draw.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="Draw code in Java">
<meta NAME="TITLE" CONTENT="Draw code in Java">
<meta NAME="KEYWORDS" CONTENT="Draw,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>Draw.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "Draw.java">Draw.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac Draw.java</span>
<span class="comment"> *  Execution:    java Draw</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  Drawing library. This class provides a basic capability for creating</span>
<span class="comment"> *  drawings with your programs. It uses a simple graphics model that</span>
<span class="comment"> *  allows you to create drawings consisting of points, lines, and curves</span>
<span class="comment"> *  in a window on your computer and to save the drawings to a file.</span>
<span class="comment"> *  This is the object-oriented version of standard draw; it supports</span>
<span class="comment"> *  multiple indepedent drawing windows.</span>
<span class="comment"> *</span>
<span class="comment"> *  Todo</span>
<span class="comment"> *  ----</span>
<span class="comment"> *    -  Add support for gradient fill, etc.</span>
<span class="comment"> *</span>
<span class="comment"> *  Remarks</span>
<span class="comment"> *  -------</span>
<span class="comment"> *    -  don't use AffineTransform for rescaling since it inverts</span>
<span class="comment"> *       images and strings</span>
<span class="comment"> *    -  careful using setFont in inner loop within an animation -</span>
<span class="comment"> *       it can cause flicker</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">BasicStroke</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Color</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">FileDialog</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Font</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">FontMetrics</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Graphics2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Image</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">MediaTracker</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">RenderingHints</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Toolkit</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseMotionListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">KeyEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">KeyListener</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Arc2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Ellipse2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">GeneralPath</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Line2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Rectangle2D</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">BufferedImage</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">DirectColorModel</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">WritableRaster</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">File</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">IOException</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">URL</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">ArrayList</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">LinkedList</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">TreeSet</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">imageio</span><span class="symbol">.</span><span class="normal">ImageIO</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">ImageIcon</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JFrame</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JLabel</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenu</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuBar</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuItem</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">KeyStroke</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Draw</span><span class="keyword">&lt;/i&gt;</span><span class="comment">. This class provides a basic capability for</span>
<span class="comment"> *  creating drawings with your programs. It uses a simple graphics model that</span>
<span class="comment"> *  allows you to create drawings consisting of points, lines, and curves</span>
<span class="comment"> *  in a window on your computer and to save the drawings to a file.</span>
<span class="comment"> *  This is the object-oriented version of standard draw; it supports</span>
<span class="comment"> *  multiple indepedent drawing windows.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/31datatype"</span><span class="keyword">&gt;</span><span class="comment">Section 3.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;i&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">Draw</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> ActionListener</span><span class="symbol">,</span><span class="normal"> MouseListener</span><span class="symbol">,</span><span class="normal"> MouseMotionListener</span><span class="symbol">,</span><span class="normal"> KeyListener </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color black.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BLACK </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">BLACK</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color blue.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BLUE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">BLUE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color cyan.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> CYAN </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">CYAN</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color dark gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DARK_GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">DARK_GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color green.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> GREEN  </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">GREEN</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color light gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> LIGHT_GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">LIGHT_GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color magenta.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> MAGENTA </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">MAGENTA</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color orange.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> ORANGE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">ORANGE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color pink.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> PINK </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">PINK</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color red.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> RED </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">RED</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color white.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> WHITE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">WHITE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color yellow.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> YELLOW </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">YELLOW</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of blue used in Introduction to Programming in Java.</span>
<span class="comment">     * It is Pantone 300U. The RGB values are approximately (9, 90, 166).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_BLUE </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">9</span><span class="symbol">,</span><span class="normal"> </span><span class="number">90</span><span class="symbol">,</span><span class="normal"> </span><span class="number">166</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of light blue used in Introduction to Programming in Java.</span>
<span class="comment">     * The RGB values are approximately (103, 198, 243).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_LIGHT_BLUE </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">103</span><span class="symbol">,</span><span class="normal"> </span><span class="number">198</span><span class="symbol">,</span><span class="normal"> </span><span class="number">243</span><span class="symbol">);</span>
<span class="normal">    </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of red used in Algorithms 4th edition.</span>
<span class="comment">     * The RGB values are (173, 32, 24).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_RED </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">173</span><span class="symbol">,</span><span class="normal"> </span><span class="number">32</span><span class="symbol">,</span><span class="normal"> </span><span class="number">24</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// default colors</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DEFAULT_PEN_COLOR   </span><span class="symbol">=</span><span class="normal"> BLACK</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DEFAULT_CLEAR_COLOR </span><span class="symbol">=</span><span class="normal"> WHITE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// boundary of drawing canvas, 0% border</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> BORDER </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_XMIN </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_XMAX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_YMIN </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_YMAX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// default canvas size is SIZE-by-SIZE</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> DEFAULT_SIZE </span><span class="symbol">=</span><span class="normal"> </span><span class="number">512</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// default pen radius</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_PEN_RADIUS </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.002</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// default font</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> DEFAULT_FONT </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Font</span><span class="symbol">(</span><span class="string">"SansSerif"</span><span class="symbol">,</span><span class="normal"> Font</span><span class="symbol">.</span><span class="normal">PLAIN</span><span class="symbol">,</span><span class="normal"> </span><span class="number">16</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// current pen color</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> penColor</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// canvas size</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> width  </span><span class="symbol">=</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="normal"> height </span><span class="symbol">=</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// current pen radius</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> penRadius</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// show we draw immediately or wait until next show?</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> xmin</span><span class="symbol">,</span><span class="normal"> ymin</span><span class="symbol">,</span><span class="normal"> xmax</span><span class="symbol">,</span><span class="normal"> ymax</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// name of window</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> name </span><span class="symbol">=</span><span class="normal"> </span><span class="string">"Draw"</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// for synchronization</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> mouseLock </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Object</span><span class="symbol">();</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> keyLock </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Object</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">// current font</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> font</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// the JLabel for drawing</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">JLabel</span><span class="normal"> draw</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// double buffered graphics</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">BufferedImage</span><span class="normal"> offscreenImage</span><span class="symbol">,</span><span class="normal"> onscreenImage</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Graphics2D</span><span class="normal"> offscreen</span><span class="symbol">,</span><span class="normal"> onscreen</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// the frame for drawing to the screen</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">JFrame</span><span class="normal"> frame </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JFrame</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">// mouse state</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> mouseX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> mouseY </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// keyboard state</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">LinkedList&lt;Character&gt;</span><span class="normal"> keysTyped </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> LinkedList</span><span class="symbol">&lt;</span><span class="normal">Character</span><span class="symbol">&gt;();</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">TreeSet&lt;Integer&gt;</span><span class="normal"> keysDown </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> TreeSet</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>

<span class="normal">    </span><span class="comment">// event-based listeners</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">ArrayList&lt;DrawListener&gt;</span><span class="normal"> listeners </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> ArrayList</span><span class="symbol">&lt;</span><span class="normal">DrawListener</span><span class="symbol">&gt;();</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty drawing object with the given name.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> name the title of the drawing window.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Draw</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> name</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">name </span><span class="symbol">=</span><span class="normal"> name</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">init</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes an empty drawing object.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">Draw</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">init</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">init</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frame </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> frame</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">        frame </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JFrame</span><span class="symbol">();</span>
<span class="normal">        offscreenImage </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_ARGB</span><span class="symbol">);</span>
<span class="normal">        onscreenImage  </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_ARGB</span><span class="symbol">);</span>
<span class="normal">        offscreen </span><span class="symbol">=</span><span class="normal"> offscreenImage</span><span class="symbol">.</span><span class="function">createGraphics</span><span class="symbol">();</span>
<span class="normal">        onscreen  </span><span class="symbol">=</span><span class="normal"> onscreenImage</span><span class="symbol">.</span><span class="function">createGraphics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setXscale</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setYscale</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">DEFAULT_CLEAR_COLOR</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setPenRadius</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setFont</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">clear</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// add antialiasing</span>
<span class="normal">        </span><span class="usertype">RenderingHints</span><span class="normal"> hints </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">RenderingHints</span><span class="symbol">(</span><span class="normal">RenderingHints</span><span class="symbol">.</span><span class="normal">KEY_ANTIALIASING</span><span class="symbol">,</span>
<span class="normal">                                                  RenderingHints</span><span class="symbol">.</span><span class="normal">VALUE_ANTIALIAS_ON</span><span class="symbol">);</span>
<span class="normal">        hints</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">RenderingHints</span><span class="symbol">.</span><span class="normal">KEY_RENDERING</span><span class="symbol">,</span><span class="normal"> RenderingHints</span><span class="symbol">.</span><span class="normal">VALUE_RENDER_QUALITY</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">addRenderingHints</span><span class="symbol">(</span><span class="normal">hints</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// frame stuff</span>
<span class="normal">        </span><span class="usertype">ImageIcon</span><span class="normal"> icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">onscreenImage</span><span class="symbol">);</span>
<span class="normal">        draw </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JLabel</span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">);</span>

<span class="normal">        draw</span><span class="symbol">.</span><span class="function">addMouseListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">        draw</span><span class="symbol">.</span><span class="function">addMouseMotionListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>

<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setContentPane</span><span class="symbol">(</span><span class="normal">draw</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">addKeyListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span><span class="normal">    </span><span class="comment">// JLabel cannot get keyboard focus</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setResizable</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            // closes all windows</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setDefaultCloseOperation</span><span class="symbol">(</span><span class="normal">JFrame</span><span class="symbol">.</span><span class="normal">DISPOSE_ON_CLOSE</span><span class="symbol">);</span><span class="normal">      </span><span class="comment">// closes only current window</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setTitle</span><span class="symbol">(</span><span class="normal">name</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setJMenuBar</span><span class="symbol">(</span><span class="function">createMenuBar</span><span class="symbol">());</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">pack</span><span class="symbol">();</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">requestFocusInWindow</span><span class="symbol">();</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the upper-left hand corner of the drawing window to be (x, y), where (0, 0) is upper left.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the number of pixels from the left</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the number of pixels from the top</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the width or height is 0 or negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setLocationOnScreen</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> y </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">();</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setLocation</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>



<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the window size to w-by-h pixels.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  w the width as a number of pixels</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  h the height as a number of pixels</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the width or height is 0 or negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setCanvasSize</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> h</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">w </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> h </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"width and height must be positive"</span><span class="symbol">);</span>
<span class="normal">        width </span><span class="symbol">=</span><span class="normal"> w</span><span class="symbol">;</span>
<span class="normal">        height </span><span class="symbol">=</span><span class="normal"> h</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">init</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// create the menu bar (changed to private)</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">JMenuBar</span><span class="normal"> </span><span class="function">createMenuBar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">JMenuBar</span><span class="normal"> menuBar </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuBar</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">JMenu</span><span class="normal"> menu </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenu</span><span class="symbol">(</span><span class="string">"File"</span><span class="symbol">);</span>
<span class="normal">        menuBar</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menu</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">JMenuItem</span><span class="normal"> menuItem1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuItem</span><span class="symbol">(</span><span class="string">" Save...   "</span><span class="symbol">);</span>
<span class="normal">        menuItem1</span><span class="symbol">.</span><span class="function">addActionListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">        menuItem1</span><span class="symbol">.</span><span class="function">setAccelerator</span><span class="symbol">(</span><span class="normal">KeyStroke</span><span class="symbol">.</span><span class="function">getKeyStroke</span><span class="symbol">(</span><span class="normal">KeyEvent</span><span class="symbol">.</span><span class="normal">VK_S</span><span class="symbol">,</span>
<span class="normal">                                Toolkit</span><span class="symbol">.</span><span class="function">getDefaultToolkit</span><span class="symbol">().</span><span class="function">getMenuShortcutKeyMask</span><span class="symbol">()));</span>
<span class="normal">        menu</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menuItem1</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> menuBar</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  User and screen coordinate systems.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the x-scale to be the default (between 0.0 and 1.0).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setXscale</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setXscale</span><span class="symbol">(</span><span class="normal">DEFAULT_XMIN</span><span class="symbol">,</span><span class="normal"> DEFAULT_XMAX</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the y-scale to be the default (between 0.0 and 1.0).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setYscale</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setYscale</span><span class="symbol">(</span><span class="normal">DEFAULT_YMIN</span><span class="symbol">,</span><span class="normal"> DEFAULT_YMAX</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the x-scale.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> min the minimum value of the x-scale</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> max the maximum value of the x-scale</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setXscale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> size </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">-</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        xmin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">        xmax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the y-scale.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> min the minimum value of the y-scale</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> max the maximum value of the y-scale</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setYscale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> size </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">-</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        ymin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">        ymax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// helper functions that scale from user coordinates to screen coordinates and back</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal">  </span><span class="function">scaleX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> width  </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">);</span><span class="normal"> </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal">  </span><span class="function">scaleY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> height </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">);</span><span class="normal"> </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> w </span><span class="symbol">*</span><span class="normal"> width  </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">abs</span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">);</span><span class="normal">  </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> h</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> h </span><span class="symbol">*</span><span class="normal"> height </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">abs</span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">);</span><span class="normal">  </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal">   </span><span class="function">userX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> xmin </span><span class="symbol">+</span><span class="normal"> x </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> width</span><span class="symbol">;</span><span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">double</span><span class="normal">   </span><span class="function">userY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> ymax </span><span class="symbol">-</span><span class="normal"> y </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> height</span><span class="symbol">;</span><span class="normal">   </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Clears the screen to the default color (white).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">clear</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">clear</span><span class="symbol">(</span><span class="normal">DEFAULT_CLEAR_COLOR</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Clears the screen to the given color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> color the color to make the background</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">clear</span><span class="symbol">(</span><span class="usertype">Color</span><span class="normal"> color</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">color</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">penColor</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Gets the current pen radius.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current pen radius</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">getPenRadius</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> penRadius</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen size to the default (.002).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenRadius</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setPenRadius</span><span class="symbol">(</span><span class="normal">DEFAULT_PEN_RADIUS</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the radius of the pen to the given size.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r the radius of the pen</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if r is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenRadius</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"pen radius must be positive"</span><span class="symbol">);</span>
<span class="normal">        penRadius </span><span class="symbol">=</span><span class="normal"> r </span><span class="symbol">*</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">BasicStroke</span><span class="normal"> stroke </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BasicStroke</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> penRadius</span><span class="symbol">,</span><span class="normal"> BasicStroke</span><span class="symbol">.</span><span class="normal">CAP_ROUND</span><span class="symbol">,</span><span class="normal"> BasicStroke</span><span class="symbol">.</span><span class="normal">JOIN_ROUND</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// BasicStroke stroke = new BasicStroke((float) penRadius);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setStroke</span><span class="symbol">(</span><span class="normal">stroke</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Gets the current pen color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current pen color</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> </span><span class="function">getPenColor</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> penColor</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen color to the default color (black).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">DEFAULT_PEN_COLOR</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen color to the given color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> color the color to make the pen</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="usertype">Color</span><span class="normal"> color</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        penColor </span><span class="symbol">=</span><span class="normal"> color</span><span class="symbol">;</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">penColor</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen color to the given RGB color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  red the amount of red (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  green the amount of green (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  blue the amount of blue (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the amount of red, green, or blue are outside prescribed range</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> red</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> green</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> blue</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">red   </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> red   </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of red must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">green </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> green </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of red must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">blue  </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> blue  </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of red must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="normal">red</span><span class="symbol">,</span><span class="normal"> green</span><span class="symbol">,</span><span class="normal"> blue</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Turns on xor mode.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">xorOn</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setXORMode</span><span class="symbol">(</span><span class="normal">DEFAULT_CLEAR_COLOR</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Turns off xor mode.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">xorOff</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setPaintMode</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Gets the current </span><span class="keyword">&lt;tt&gt;</span><span class="comment">JLabel</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> for use in some other GUI.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current </span><span class="keyword">&lt;tt&gt;</span><span class="comment">JLabel</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">JLabel</span><span class="normal"> </span><span class="function">getJLabel</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> draw</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Gets the current font.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current font</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> </span><span class="function">getFont</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> font</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the font to the default font (sans serif, 16 point).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setFont</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">DEFAULT_FONT</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the font to the given value.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> font the font</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setFont</span><span class="symbol">(</span><span class="usertype">Font</span><span class="normal"> font</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">this</span><span class="symbol">.</span><span class="normal">font </span><span class="symbol">=</span><span class="normal"> font</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing geometric shapes.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a line from (x0, y0) to (x1, y1).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x0 the x-coordinate of the starting point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y0 the y-coordinate of the starting point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x1 the x-coordinate of the destination point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y1 the y-coordinate of the destination point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">line</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x0</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y0</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> x1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Line2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x0</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y0</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x1</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y1</span><span class="symbol">)));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws one pixel at (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the x-coordinate of the pixel</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the y-coordinate of the pixel</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">((</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">)),</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a point at (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the x-coordinate of the point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the y-coordinate of the point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">point</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> penRadius</span><span class="symbol">;</span>
<span class="normal">        </span><span class="comment">// double ws = factorX(2*r);</span>
<span class="normal">        </span><span class="comment">// double hs = factorY(2*r);</span>
<span class="normal">        </span><span class="comment">// if (ws &lt;= 1 &amp;&amp; hs &lt;= 1) pixel(x, y);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> r</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> r</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> r</span><span class="symbol">,</span><span class="normal"> r</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a circle of radius r, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r the radius of the circle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the radius of the circle is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">circle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"circle radius can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled circle of radius r, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r the radius of the circle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the radius of the circle is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledCircle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"circle radius can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws an ellipse with given semimajor and semiminor axes, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMajorAxis is the semimajor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMinorAxis is the semiminor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either of the axes are negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">ellipse</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMajorAxis</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMinorAxis</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">semiMajorAxis </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semimajor axis can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">semiMinorAxis </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semiminor axis can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMajorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMinorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws an ellipse with given semimajor and semiminor axes, centered on (x, y).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMajorAxis is the semimajor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMinorAxis is the semiminor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either of the axes are negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledEllipse</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMajorAxis</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMinorAxis</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">semiMajorAxis </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semimajor axis can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">semiMinorAxis </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semiminor axis can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMajorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMinorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws an arc of radius r, centered on (x, y), from angle1 to angle2 (in degrees).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r the radius of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  angle1 the starting angle. 0 would mean an arc beginning at 3 o'clock.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  angle2 the angle at the end of the arc. For example, if</span>
<span class="comment">     *         you want a 90 degree arc, then angle2 should be angle1 + 90.</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the radius of the circle is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">arc</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> angle2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"arc radius can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">angle2 </span><span class="symbol">&lt;</span><span class="normal"> angle1</span><span class="symbol">)</span><span class="normal"> angle2 </span><span class="symbol">+=</span><span class="normal"> </span><span class="number">360</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Arc2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">,</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> angle2 </span><span class="symbol">-</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> Arc2D</span><span class="symbol">.</span><span class="normal">OPEN</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a square of side length 2r, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r radius is half the length of any side of the square</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if r is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">square</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"square side length can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled square of side length 2r, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  r radius is half the length of any side of the square</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if r is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledSquare</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> r</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"square side length can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">r</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a rectangle of given half width and half height, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfWidth is half the width of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfHeight is half the height of the rectangle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if halfWidth or halfHeight is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">rectangle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">halfWidth  </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half width can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">halfHeight </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half height can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled rectangle of given half width and half height, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the x-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the y-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfWidth is half the width of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfHeight is half the height of the rectangle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if halfWidth or halfHeight is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledRectangle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">halfWidth  </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half width can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">halfHeight </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half height can't be negative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a polygon with the given (x[i], y[i]) coordinates.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x an array of all the x-coordindates of the polygon</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y an array of all the y-coordindates of the polygon</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">polygon</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">GeneralPath</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">GeneralPath</span><span class="symbol">();</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">moveTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">lineTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]));</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">closePath</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled polygon with the given (x[i], y[i]) coordinates.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x an array of all the x-coordindates of the polygon</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y an array of all the y-coordindates of the polygon</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledPolygon</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> N </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">GeneralPath</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">GeneralPath</span><span class="symbol">();</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">moveTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> N</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">lineTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]));</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">closePath</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>



<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing images.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">// get an image from the given filename</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="usertype">Image</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// to read from file</span>
<span class="normal">        </span><span class="usertype">ImageIcon</span><span class="normal"> icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// try to read from URL</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">icon </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">.</span><span class="function">getImageLoadStatus</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> MediaTracker</span><span class="symbol">.</span><span class="normal">COMPLETE</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">URL</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">                icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="comment">/* not a url */</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// in case file is inside a .jar</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">icon </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">.</span><span class="function">getImageLoadStatus</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> MediaTracker</span><span class="symbol">.</span><span class="normal">COMPLETE</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> Draw</span><span class="symbol">.</span><span class="keyword">class</span><span class="symbol">.</span><span class="function">getResource</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" not found"</span><span class="symbol">);</span>
<span class="normal">            icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> icon</span><span class="symbol">.</span><span class="function">getImage</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws picture (gif, jpg, or png) centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center x-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center y-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  s the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image is corrupt</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws picture (gif, jpg, or png) centered on (x, y),</span>
<span class="comment">     * rotated given number of degrees.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center x-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center y-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  s the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image is corrupt</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>

<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center x coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center y coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  s the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  w the width of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  h the height of the image</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image is corrupt</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> h</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="normal">h</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ws</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">hs</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws picture (gif, jpg, or png) centered on (x, y), rotated</span>
<span class="comment">     * given number of degrees, rescaled to w-by-h.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center x-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center y-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  s the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  w the width of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  h the height of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image is corrupt</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> w</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> h</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="normal">h</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ws</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">hs</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>

<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing text.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Writes the given text string in the current font, centered on (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the center x-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the center y-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the text</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">text</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">font</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">FontMetrics</span><span class="normal"> metrics </span><span class="symbol">=</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">getFontMetrics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">stringWidth</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">getDescent</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawString</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">+</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Writes the given text string in the current font, centered on (x, y) and</span>
<span class="comment">     * rotated by the specified number of degrees.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the center x-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the center y-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">text</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">text</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Writes the given text string in the current font, left-aligned at (x, y).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the x-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the y-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the text</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">textLeft</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">font</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">FontMetrics</span><span class="normal"> metrics </span><span class="symbol">=</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">getFontMetrics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// int ws = metrics.stringWidth(s);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">getDescent</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawString</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">+</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Displays on screen, pause for </span><span class="keyword">&lt;tt&gt;</span><span class="comment">t</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> milliseconds, and turn on</span>
<span class="comment">     * </span><span class="keyword">&lt;em&gt;</span><span class="comment">animation mode</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * Subsequent calls to drawing methods such as </span><span class="keyword">&lt;tt&gt;</span><span class="comment">line()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">circle()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">square()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> will not be displayed on screen until the next call to </span><span class="keyword">&lt;tt&gt;</span><span class="comment">show()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * This is useful for producing animations (clear the screen, draw a bunch of shapes,</span>
<span class="comment">     * display on screen for a fixed amount of time, and repeat). It also speeds up</span>
<span class="comment">     * drawing a huge number of shapes (call </span><span class="keyword">&lt;tt&gt;</span><span class="comment">show(0)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to defer drawing</span>
<span class="comment">     * on screen, draw the shapes, and call </span><span class="keyword">&lt;tt&gt;</span><span class="comment">show(0)</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> to display them all</span>
<span class="comment">     * on screen at once).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> t number of milliseconds</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            Thread</span><span class="symbol">.</span><span class="function">sleep</span><span class="symbol">(</span><span class="normal">t</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">InterruptedException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Error sleeping"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Displays on-screen and turn off animation mode.</span>
<span class="comment">     * Subsequent calls to drawing methods such as </span><span class="keyword">&lt;tt&gt;</span><span class="comment">line()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">circle()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">square()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> will be displayed on screen when called. This is the default.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// draw onscreen if defer is false</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">draw</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">defer</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        onscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">offscreenImage</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">repaint</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Saves this drawing to a file.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the file (with suffix png, jpg, or gif)</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">save</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">File</span><span class="normal"> file </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">File</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> suffix </span><span class="symbol">=</span><span class="normal"> filename</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">.</span><span class="function">lastIndexOf</span><span class="symbol">(</span><span class="string">'.'</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// png files</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">toLowerCase</span><span class="symbol">().</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"png"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                ImageIO</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">offscreenImage</span><span class="symbol">,</span><span class="normal"> suffix</span><span class="symbol">,</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// need to change from ARGB to RGB for jpeg</span>
<span class="normal">        </span><span class="comment">// reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&amp;L=java2d-interest&amp;D=0&amp;P=2727</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">toLowerCase</span><span class="symbol">().</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"jpg"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">WritableRaster</span><span class="normal"> raster </span><span class="symbol">=</span><span class="normal"> offscreenImage</span><span class="symbol">.</span><span class="function">getRaster</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">WritableRaster</span><span class="normal"> newRaster</span><span class="symbol">;</span>
<span class="normal">            newRaster </span><span class="symbol">=</span><span class="normal"> raster</span><span class="symbol">.</span><span class="function">createWritableChild</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="cbracket">{</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">2</span><span class="cbracket">}</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">DirectColorModel</span><span class="normal"> cm </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">DirectColorModel</span><span class="symbol">)</span><span class="normal"> offscreenImage</span><span class="symbol">.</span><span class="function">getColorModel</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">DirectColorModel</span><span class="normal"> newCM </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DirectColorModel</span><span class="symbol">(</span><span class="normal">cm</span><span class="symbol">.</span><span class="function">getPixelSize</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getRedMask</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getGreenMask</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getBlueMask</span><span class="symbol">());</span>
<span class="normal">            </span><span class="usertype">BufferedImage</span><span class="normal"> rgbBuffer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">newCM</span><span class="symbol">,</span><span class="normal"> newRaster</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">,</span><span class="normal">  </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                ImageIO</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">rgbBuffer</span><span class="symbol">,</span><span class="normal"> suffix</span><span class="symbol">,</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Invalid image file type: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> suffix</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">actionPerformed</span><span class="symbol">(</span><span class="usertype">ActionEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">FileDialog</span><span class="normal"> chooser </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">FileDialog</span><span class="symbol">(</span><span class="normal">frame</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"Use a .png or .jpg extension"</span><span class="symbol">,</span><span class="normal"> FileDialog</span><span class="symbol">.</span><span class="normal">SAVE</span><span class="symbol">);</span>
<span class="normal">        chooser</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> filename </span><span class="symbol">=</span><span class="normal"> chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">filename </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="function">save</span><span class="symbol">(</span><span class="normal">chooser</span><span class="symbol">.</span><span class="function">getDirectory</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> File</span><span class="symbol">.</span><span class="normal">separator </span><span class="symbol">+</span><span class="normal"> chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>



<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Event-based interactions.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Adds a {</span><span class="type">@link</span><span class="comment"> DrawListener} to listen to keyboard and mouse events.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> listener the {\tt DrawListener} argument</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">addListener</span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// ensure there is a window for listenting to events</span>
<span class="normal">        </span><span class="function">show</span><span class="symbol">();</span>
<span class="normal">        listeners</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">listener</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">addKeyListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">addMouseListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">addMouseMotionListener</span><span class="symbol">(</span><span class="keyword">this</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setFocusable</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span><span class="normal"> </span>
<span class="normal">    </span><span class="cbracket">}</span>




<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Mouse interactions.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the mouse is being pressed.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the mouse is being pressed;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">mousePressed</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mousePressed</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the x-coordinate of the mouse.</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the x-coordinate of the mouse</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">mouseX</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mouseX</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the y-coordinate of the mouse.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the y-coordinate of the mouse</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">mouseY</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mouseY</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>



<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseClicked</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseEntered</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseExited</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mousePressed</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">            mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getButton</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> MouseEvent</span><span class="symbol">.</span><span class="normal">BUTTON1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">                listener</span><span class="symbol">.</span><span class="function">mousePressed</span><span class="symbol">(</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">()),</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">()));</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseReleased</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getButton</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">==</span><span class="normal"> MouseEvent</span><span class="symbol">.</span><span class="normal">BUTTON1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">                listener</span><span class="symbol">.</span><span class="function">mouseReleased</span><span class="symbol">(</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">()),</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">()));</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseDragged</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal">  </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="comment">// doesn't seem to work if a button is specified</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">            listener</span><span class="symbol">.</span><span class="function">mouseDragged</span><span class="symbol">(</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">()),</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">()));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseMoved</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> </span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Keyboard interactions.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the user has typed a key.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the user has typed a key; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextKeyTyped</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">keysTyped</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * The next key typed by the user.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next key typed by the user</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">char</span><span class="normal"> </span><span class="function">nextKeyTyped</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> keysTyped</span><span class="symbol">.</span><span class="function">removeLast</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the keycode is being pressed.</span>
<span class="comment">     * </span><span class="keyword">&lt;p&gt;</span>
<span class="comment">     * This method takes as an argument the keycode (corresponding to a physical key).</span>
<span class="comment">     * It can handle action keys (such as F1 and arrow keys) and modifier keys</span>
<span class="comment">     * (such as shift and control).</span>
<span class="comment">     * See {</span><span class="type">@link</span><span class="comment"> KeyEvent} for a description of key codes.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  keycode the keycode to check</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if </span><span class="keyword">&lt;tt&gt;</span><span class="comment">keycode</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> is currently being pressed;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isKeyPressed</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> keycode</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> keysDown</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">keycode</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyTyped</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysTyped</span><span class="symbol">.</span><span class="function">addFirst</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyChar</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// notify all listeners</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">            listener</span><span class="symbol">.</span><span class="function">keyTyped</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyChar</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyPressed</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysDown</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// notify all listeners</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">            listener</span><span class="symbol">.</span><span class="function">keyPressed</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyReleased</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysDown</span><span class="symbol">.</span><span class="function">remove</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// notify all listeners</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">DrawListener</span><span class="normal"> listener </span><span class="symbol">:</span><span class="normal"> listeners</span><span class="symbol">)</span>
<span class="normal">            listener</span><span class="symbol">.</span><span class="function">keyPressed</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">    </span><span class="cbracket">}</span>




<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Test client.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// create one drawing window</span>
<span class="normal">        </span><span class="usertype">Draw</span><span class="normal"> draw1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Draw</span><span class="symbol">(</span><span class="string">"Test client 1"</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">square</span><span class="symbol">(.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">filledSquare</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">circle</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">Draw</span><span class="symbol">.</span><span class="normal">MAGENTA</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">(.</span><span class="number">02</span><span class="symbol">);</span>
<span class="normal">        draw1</span><span class="symbol">.</span><span class="function">arc</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">200</span><span class="symbol">,</span><span class="normal"> </span><span class="number">45</span><span class="symbol">);</span>


<span class="normal">        </span><span class="comment">// create another one</span>
<span class="normal">        </span><span class="usertype">Draw</span><span class="normal"> draw2 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Draw</span><span class="symbol">(</span><span class="string">"Test client 2"</span><span class="symbol">);</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">setCanvasSize</span><span class="symbol">(</span><span class="number">900</span><span class="symbol">,</span><span class="normal"> </span><span class="number">200</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// draw a blue diamond</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">();</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">Draw</span><span class="symbol">.</span><span class="normal">BLUE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">filledPolygon</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// text</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">Draw</span><span class="symbol">.</span><span class="normal">BLACK</span><span class="symbol">);</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">text</span><span class="symbol">(</span><span class="number">0.2</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.5</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"bdfdfdfdlack text"</span><span class="symbol">);</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">Draw</span><span class="symbol">.</span><span class="normal">WHITE</span><span class="symbol">);</span>
<span class="normal">        draw2</span><span class="symbol">.</span><span class="function">text</span><span class="symbol">(</span><span class="number">0.8</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.8</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"white text"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
